package com.psl.services.frame;

import com.psl.Dao.Conn;
import com.psl.model.position;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class accessrole {
    public List queryrole() {
        Connection conn = new Conn().getConn();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        List role = new ArrayList();
        try {
            pstmt = conn.prepareStatement("SELECT position_id,position FROM positionlist");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                position pt = new position();
                pt.setposition_id(rs.getInt(1));
                pt.setposition(rs.getString(2));
                role.add(pt);
                HttpServletRequest request = ServletActionContext.getRequest();
                request.setAttribute("role", role);
            }
            return role;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public boolean delrole(int position_id) {
        Connection conn = new Conn().getConn();
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("DELETE from positionlist WHERE position_id=?");
            pstmt.setInt(1, position_id);
            pstmt.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }


}
